/*
 * Order.java
 *
 * Created on April 1, 2007, 2:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model;

import java.util.*;

/**
 *
 * @author Alirio
 */
public class Order {
  
  public static final String STATUS_PEN = "PEN";
  
  private long id;
  private String date;
  private String time;
  private Customer customer;
  private List products = new ArrayList();
  private double total;
  private Salesman salesman;
  private String status;
  private String statusId;
  private String comments;
  private String requiredDate;
  
  //para fines de la app flex
  private double amount; 
  private String statusChangedAt;
  private String statusChangedOn;
  
  /** Creates a new instance of Order */
  public Order() {
  }
  
  private void computeTotal() {
    double acum = 0;
    for (Object elem : products) {
      acum += ((LineItem) elem).getSubtotal();
    }
    total = acum;
  }
  
  public double getTotalBoxes() {
    double acum = 0;
    for (Object elem : products) {
      LineItem li = (LineItem) elem;
      Product p = li.getProduct();
      if (li.getUnit().equals(p.getAlternativeUnit())){
        acum = acum + li.getQuantity();
      } else {
        if (p.getConversionRate() != 0.00) {
          acum = acum + (li.getQuantity() / p.getConversionRate());
        }
      }
    }
    return acum;
  }
  
  public void addProduct(LineItem li) {
    products.add(li);
  }
  
  public void addProduct(Product p, double qty, String unit, boolean outOfStock) {
    addProduct(new LineItem(p, qty, unit, outOfStock));
  }
  
  public boolean removeLineItem(LineItem li) {
    return products.remove(li);
  }
  
  public LineItem getLineItemByIdAndUnit(String id, String unit) {
    LineItem item = null;
    boolean x = false;
    for (Object elem : products) {
      item = (LineItem) elem;
      if (item.getProduct().getId().equals(id) && item.getUnit().equals(unit)) {
        x = true;
        break;
      }
    }
    
    if (x) {
      return item;
    } else {
      return null;
    }
    
  }
  
  public long getId() {
    return id;
  }
  
  public void setId(long id) {
    this.id = id;
  }
  
  public String getDate() {
    return date;
  }
  
  public void setDate(String date) {
    this.date = date;
  }
  
  public String getTime() {
    return time;
  }
  
  public void setTime(String time) {
    this.time = time;
  }
  
  public Customer getCustomer() {
    return customer;
  }
  
  public void setCustomer(Customer customer) {
    this.customer = customer;
  }
  
  public List getProducts() {
    return products;
  }
  
  public void setProducts(List items) {
    this.products = items;
  }
  
  public double getTotal() {
    computeTotal();
    return total;
  }
  
  public String toString() {
    return String.valueOf(products.size());
  }
  
  public Salesman getSalesman() {
    return salesman;
  }
  
  public void setSalesman(Salesman salesman) {
    this.salesman = salesman;
  }
  
  public String getStatus() {
    return status;
  }
  
  public void setStatus(String status) {
    this.status = status;
  }

  public String getStatusId() {
    return statusId;
  }

  public void setStatusId(String statusId) {
    this.statusId = statusId;
  }

  public String getComments() {
    return comments;
  }

  public void setComments(String comments) {
    this.comments = comments;
  }

    public String getRequiredDate() {
        return requiredDate;
    }

    public void setRequiredDate(String requiredDate) {
        this.requiredDate = requiredDate;
    }

  public double getAmount() {
    return amount;
  }

  public void setAmount(double amount) {
    this.amount = amount;
  }

  public String getStatusChangedAt() {
    return statusChangedAt;
  }

  public void setStatusChangedAt(String statusChangedAt) {
    this.statusChangedAt = statusChangedAt;
  }

  public String getStatusChangedOn() {
    return statusChangedOn;
  }

  public void setStatusChangedOn(String statusChangedOn) {
    this.statusChangedOn = statusChangedOn;
  }
  
}
